public class SqListClass<E> {
    final int initcapacity = 10;
    public E[] data;
    public int size;
    private int capacity;

    public SqListClass() {
        data = (E[]) new Object[initcapacity];
        capacity = initcapacity;
        size = 0;
    }

    // 获取指定索引位置的元素
    public E GetElem(int i) {
        if (i < 0 || i > size - 1) {
            throw new IllegalArgumentException("查找：位置i不在有效范围内");
        }
        return (E) data[i];
    }

    // 返回顺序表的当前元素个数
    public int size() {
        return size;
    }

    //设置
    public void SetElem(int i, E e) {
        if (i < 0 || i > size - 1)
            throw new IllegalArgumentException("设置：位置i不在有效范围内");
        data[i] = e;
    }

    public void Setsize(int nlen) {
        if (nlen < 0 || nlen > size)
            throw new IllegalArgumentException("设置长度:n不在有效范围内");
        size = nlen;
    }

    public void swap(int i, int j) {
        E tmp = data[i];
        data[i] = data[j];
        data[j] = tmp;
    }

    // 向顺序表中添加元素
    public void add(E element) {
        if (size == capacity) {
            // 扩容操作
            capacity *= 2;
            E[] newData = (E[]) new Object[capacity];
            System.arraycopy(data, 0, newData, 0, size);
            data = newData;
        }
        data[size++] = element;
    }

    public void updatecapacity(int newcapacity){
        E[] newdata = (E[])new Object[newcapacity];
        for (int i = 0;i<size;i++)
            newdata[i]=data[i];
        capacity=newcapacity;
        data=newdata;
    }

    public void Add(E e){
        if(size==capacity)
            updatecapacity(2*size);
        data[size]=e;
        size++;
    }


    public void clear() {
    }
    public String toString(){
        String ans="";
        for(int i=0;i<size;i++)
            ans+=data[i].toString()+" ";
        return ans;
    }
}
